clear

import delimited "Data_prepared_for_frm_in_STATA\post_analysis_data_candidates_long.csv"

drop if share_of_tweets_for_each_policy_ == "NA"

// egen share_of_tweets_for_each_pol_f = group(share_of_tweets_for_each_policy_)
// encode share_of_tweets_for_each_policy_, gen(share_of_tweets_for_each_pol_f)
// sum share_of_tweets_for_each_pol_f

generate share_of_tweets_for_each_pol_f = real(share_of_tweets_for_each_policy_)


// linear model
reg share_of_tweets_for_each_pol_f share_in_manifesto, robust

// output regression table
outreg2 using tab_a7_results_issue_share.doc, replace ctitle(Linear)

// estimate fractional logit model using the user-written frm command
frm share_of_tweets_for_each_pol_f share_in_manifesto, linkfrac(loglog)
estimates store frac_loglog

// output regression table
outreg2 using tab_a7_results_issue_share.doc, append ctitle(Model 1)

// perform RESET test using two powers
frm_reset, l(2)

// perform GOF tests 
frm_ggoff // Stat 2.188   pvalue 0.1391

// compute APE
margins, dydx(share_in_manifesto)
// generate plot
margins, at(share_in_manifesto = (0 (0.01) 0.23))    saving("Data_frm_results_from_STATA\individual_data_margins.dta")
marginsplot



clear

import delimited "Data_prepared_for_frm_in_STATA\post_analysis_data_candidates_long.csv"

drop if share_of_tweets_for_each_policy_ == "NA"

// egen share_of_tweets_for_each_pol_f = group(share_of_tweets_for_each_policy_)
// encode share_of_tweets_for_each_policy_, gen(share_of_tweets_for_each_pol_f)
// sum share_of_tweets_for_each_pol_f

generate share_of_tweets_for_each_pol_f = real(share_of_tweets_for_each_policy_)


// subgroup top issue = 0

drop if top_issue == 1

// estimate fractional logit model using the user-written frm command
frm share_of_tweets_for_each_pol_f share_in_manifesto, linkfrac(loglog)
estimates store frac_loglog

// output regression table
outreg2 using tab_a7_results_issue_share.doc, append ctitle(Model 2)

// perform RESET test using two powers
frm_reset, l(2)

// perform GOF tests
frm_ggoff //  Stat 17.617   pvalue 0.0000

// compute APE
margins, dydx(share_in_manifesto)
// generate plot
margins, at(share_in_manifesto = (0 (0.01) 0.23))    saving("Data_frm_results_from_STATA\individual_data_margins_subgroup_top_issue_0.dta")
marginsplot


// subgroup top issue = 1
clear
import delimited "Data_prepared_for_frm_in_STATA\post_analysis_data_candidates_long.csv"
drop if share_of_tweets_for_each_policy_ == "NA"
generate share_of_tweets_for_each_pol_f = real(share_of_tweets_for_each_policy_)

drop if top_issue == 0

// estimate fractional logit model using the user-written frm command
frm share_of_tweets_for_each_pol_f share_in_manifesto, linkfrac(loglog)
estimates store frac_loglog

// output regression table
outreg2 using tab_a7_results_issue_share.doc, append ctitle(Model 3)

// perform RESET test using two powers
frm_reset, l(2)

// perform GOF tests
frm_ggoff // Stat 27.852   pvalue 0.0000

// compute APE
margins, dydx(share_in_manifesto)
// generate plot

margins, at(share_in_manifesto = (0 (0.01) 0.23))    saving("Data_frm_results_from_STATA\individual_data_margins_subgroup_top_issue_1.dta")
marginsplot
